package com.dianjing.dianqun.utils;

import java.util.Properties;

import org.csource.common.NameValuePair;
import org.csource.fastdfs.ClientGlobal;
import org.csource.fastdfs.StorageClient1;
import org.csource.fastdfs.StorageServer;
import org.csource.fastdfs.TrackerClient;
import org.csource.fastdfs.TrackerServer;
import org.springframework.context.annotation.ComponentScan;

/**
 * @date : 2017年10月24日
 * 
 * @author: baoyongtao
 * 
 * @see :
 * 
 */

@ComponentScan
public class FastDFSClient {

	private static TrackerClient trackerClient = null;
	private static TrackerServer trackerServer = null;
	private static StorageClient1 storageClient = null;
	private static StorageServer storageServer = null;
	private static String conf = "classpath:fdfs_client.conf";
	
	static {
		try {
			//springboot 项目发布jar文件 时和开发工具处理不一样
			try {
				ClientGlobal.init(conf);
			} catch (Exception e) {
				conf = conf.replace("classpath:", FastDFSClient.class.getClass().getResource("/").getPath());
				ClientGlobal.init(conf);
			}
			trackerClient = new TrackerClient();
			trackerServer = trackerClient.getConnection();
			storageClient = new StorageClient1(trackerServer, storageServer);
		} catch (Exception e) {
			System.out.println("加载配置文件失败");
		}
	}
	
	private FastDFSClient(){}

	public static String uploadFile(String fileName, String extName, NameValuePair[] metas) throws Exception {
		String result = storageClient.upload_file1(fileName, extName, metas);
		return result;
	}

	public static String uploadFile(String fileName) throws Exception {
		String result = storageClient.upload_file1(fileName, null, null);
		return result;
	}

	public static String uploadFile(String fileName, String extName) throws Exception {
		String result = storageClient.upload_file1(fileName, extName, null);
		return result;
	}
	public static String uploadFile(byte[] file_buff, String extName,NameValuePair[] metas) throws Exception {
		return storageClient.upload_file1(file_buff, extName, metas);
	}
}
